package com.ruoyi.dm.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.dm.domain.SpaceTargetMeasureParam;
import com.ruoyi.dm.domain.SpaceTargetMeasureVO;
import com.ruoyi.dm.domain.po.SpaceTargetMeasurePO;
import com.ruoyi.dm.mapper.SpaceTargetMeasureMapper;
import com.ruoyi.dm.service.ISpaceTargetMeasureService;
import com.ruoyi.utils.TranferUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SpaceTargetMeasureImpl extends ServiceImpl<SpaceTargetMeasureMapper, SpaceTargetMeasurePO> implements ISpaceTargetMeasureService {

    @Autowired
    private SpaceTargetMeasureMapper measureMapper;

    @Override
    public IPage<SpaceTargetMeasureVO> query(SpaceTargetMeasureParam queryParam) {
        LambdaQueryWrapper<SpaceTargetMeasurePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(queryParam.getName()),SpaceTargetMeasurePO::getName,queryParam.getName());
        queryWrapper.le(null != queryParam.getStartDate() && null != queryParam.getEndDate() ,SpaceTargetMeasurePO::getCreateDate,queryParam.getStartDate());
        queryWrapper.ge(null != queryParam.getStartDate() && null != queryParam.getEndDate() ,SpaceTargetMeasurePO::getCreateDate,queryParam.getEndDate());
        queryWrapper.orderByAsc(SpaceTargetMeasurePO::getCreateDate);
        IPage page = page(queryParam.page(), queryWrapper);
        List<SpaceTargetMeasureVO> objects = new ArrayList<>();
        for (Object airCollectRecordPO : page.getRecords()) {
            SpaceTargetMeasurePO po = (SpaceTargetMeasurePO) airCollectRecordPO;
            SpaceTargetMeasureVO airCollectVO = TranferUtils.tranferSpaceTargetMeasurePO(po);
            objects.add(airCollectVO);
        }
        page.setRecords(objects);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean input(MultipartFile[] files) {
        try {
            for (MultipartFile multipartFile : files) {
                InputStream inputStream = multipartFile.getInputStream();
                ExcelUtil<SpaceTargetMeasurePO> excelUtil = new ExcelUtil<SpaceTargetMeasurePO>(SpaceTargetMeasurePO.class);
                List<SpaceTargetMeasurePO> pos = excelUtil.importExcel(inputStream, 0);
                if (null != pos) {
                    Date now = new Date();
                    for (SpaceTargetMeasurePO po : pos) {
                        po.setCreateDate(now);
                    }
                    saveBatch(pos);
                }
            }
            //剔除重复的
            measureMapper.deleteRepeate();
        } catch (Exception e) {
            log.error("导入异常",e);
            throw new ServiceException("导入异常");
        }
        return false;
    }

    @Override
    public boolean delFile(Long id) {
        removeById(id);
        return true;
    }

    @Override
    public boolean delBatch(List<Long> delParam) {
        removeBatchByIds(delParam);
        return true;
    }

    @Override
    public String export() {
        LambdaQueryWrapper<SpaceTargetMeasurePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(SpaceTargetMeasurePO::getCreateDate);
        List<SpaceTargetMeasurePO> radioStationPOS = list(queryWrapper);
        ExcelUtil<SpaceTargetMeasurePO> excelUtil = new ExcelUtil<SpaceTargetMeasurePO>(SpaceTargetMeasurePO.class);
        AjaxResult result = excelUtil.exportExcel(radioStationPOS, "空间目标测量数据");
        String filename =(String) result.get("data");
        String filePath = RuoYiConfig.getDownloadPath() + filename;
        return filePath;
    }

    @Override
    public boolean edit(SpaceTargetMeasureVO vo)  {
        SpaceTargetMeasurePO po = new SpaceTargetMeasurePO();
        po.setCode(vo.getCode());
        po.setName(vo.getName());
        po.setDens(vo.getDens());
        po.setLightIndex(vo.getLightIndex());
        po.setLightArea(vo.getLightArea());
        po.setFIndex(vo.getFIndex());
        po.setWindArea(vo.getWindArea());
        po.setAirDens(vo.getAirDens());
        if (null != vo.getId()) {
            po.setId(vo.getId());
            updateById(po);
        }else {
            po.setCreateDate(new Date());
            save(po);
        }
        return true;
    }

    @Override
    public String modelExport() {
        File template = new File(RuoYiConfig.getDownloadPath() + "空间目标测量数据.xls");
        if (template.isFile()) {
            //存在模板 直接返回
        }else {
            ExcelUtil<SpaceTargetMeasurePO> excelUtil = new ExcelUtil<SpaceTargetMeasurePO>(SpaceTargetMeasurePO.class);
            AjaxResult result = excelUtil.exportExcel(ListUtil.empty(), "空间目标测量数据");
            String filename = (String) result.get("data");
            String filePath = RuoYiConfig.getDownloadPath() + filename;
            FileUtil.copy(new File(filePath), template, true);
        }
        return template.getAbsolutePath();
    }
}
